#include <bits/stdc++.h>
using namespace std;
#include <atcoder/mincostflow>
using namespace atcoder;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 55;
const int MX = 1e9;

int n, m;
int a[N][N];
char s[N][N];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) rep(j, 1, n) cin >> a[i][j];
  int S = n * 2 + 1, T = n * 2 + 2;
  mcf_graph<int, ll> graph(n * 2 + 3);
  graph.add_edge(S, T, n * m, MX);
  rep(i, 1, n) graph.add_edge(S, i, m, 0);
  rep(i, 1, n) graph.add_edge(i + n, T, m, 0);
  rep(i, 1, n) rep(j, 1, n) graph.add_edge(i, j + n, 1, MX - a[i][j]);
  graph.flow(S, T, n * m);
  const auto& edges = graph.edges();
  ll ans = 0;
  rep(i, 1, n) rep(j, 1, n) s[i][j] = '.';
  for (auto& e : edges) {
    if (e.from == S || e.to == T || e.flow == 0) continue;
    s[e.from][e.to - n] = 'X';
    ans += a[e.from][e.to - n];
  }
  cout << ans << endl;
  rep(i, 1, n) {
    rep(j, 1, n) cout << s[i][j];
    cout << endl;
  }
  return 0;
}